//
//  KABLoginModel.m
//  kabu
//
//  Created by XiangDongMBP on 2017/1/24.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABLoginModel.h"
#import "KABCosplayModel.h"

@implementation KABReceiverInfoItem
- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }

+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"receiverId" : @"id"};
}
@end
@implementation KABReceiverInfoList
- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }

+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"receiver_info_list" : [KABReceiverInfoItem class]};
}
- (KABReceiverInfoItem *)selReceiverInfoItem {
    __block KABReceiverInfoItem *item = nil;
    [self.receiver_info_list enumerateObjectsUsingBlock:^(KABReceiverInfoItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.receiverId == self.selReceiverId) {
            item = obj;
            *stop = YES;
        }
    }];
    if (!item) {
        return self.defaultReceiverInfoItem;
    }
    return item;
}
- (KABReceiverInfoItem *)defaultReceiverInfoItem {
    __block KABReceiverInfoItem *item = nil;
    [self.receiver_info_list enumerateObjectsUsingBlock:^(KABReceiverInfoItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.is_default) {
            item = obj;
            *stop = YES;
        }
    }];
    return item;
}
- (BOOL)totalLoad {
    return (self.receiver_info_list.count >= self.total_rows);
}
- (void)setDefaultReceiverWithRecId:(NSInteger)recId {
    [self.receiver_info_list enumerateObjectsUsingBlock:^(KABReceiverInfoItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.is_default = (obj.receiverId == recId);
    }];
}

@end


@implementation KABLoginMiscInfo
- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }
@end

@implementation KABLoginModel
- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }

+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"uid" : @"id"};
}
- (NSString *)sexStr {
    NSArray* array = @[@"女", @"男", @"未知"];
    if (self.sex < array.count) {
        return array[self.sex];
    }
    return array.lastObject;
}
- (NSString *)areaStr {
    return [NSString stringWithFormat:@"%@ %@", self.province, self.city];
}
- (NSString *)uidStr {
    return [NSString stringWithFormat:@"%zd", self.uid];
}
- (NSString *)sign {
    if (!_sign) {
        _sign = @" ";
    }
    return _sign;
}
- (void)updateInfoWithMineInfo:(KABMineInfo *)info {
    if (!info) {
        return;
    }
    self.nick_name = info.nick_name;
    self.headimg_url = info.headimg_url;
    self.sign = info.sign;
    self.sex = info.sex;
    self.misc_detail.total_fans = info.total_fans;
    self.misc_detail.total_follow = info.total_follow;
    self.misc_detail.total_collect_cosplay = info.total_collect_cosplay;
    self.misc_detail.is_merchant = info.is_merchant;
    self.misc_detail.is_celebrity = info.is_celebrity;
    self.misc_detail.ranking_id = info.ranking_id;
}
@end


@implementation KABMineUserItem
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"user_id" : @[@"id", @"user_id"]};
}
@end
@implementation KABMineUserList
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"user_list" : @[@"fan_list", @"follow_list", @"users"]};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"user_list" : [KABMineUserItem class]};
}
- (BOOL)totalLoad {
    return (self.user_list.count >= self.total_rows);
}
- (void)addMoreWithData:(KABMineUserList *)temp {
    self.total_rows = temp.total_rows;
    [self.user_list addObjectsFromArray:temp.user_list];
}

@end


@implementation KABMineMsgItem
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"msgId" : @"id"};
}
@end
@implementation KABMineMsgList
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"message_list" : [KABMineMsgItem class]};
}
- (BOOL)totalLoad {
    return (self.message_list.count >= self.total_rows);
}
- (void)addMoreWithData:(KABMineMsgList *)temp {
    self.total_rows = temp.total_rows;
    [self.message_list addObjectsFromArray:temp.message_list];
}
@end


@implementation KABMineNewMsg
- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }
- (NSInteger)totalNewMsgCnt {
    return self.new_praise_number + self.new_reply_number + self.new_comment_number;
}
@end


@implementation KABMineHpUserDetail

@end
@implementation KABMineHomepage
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"cosplay_list" : [KABCosListItem class]};
}
- (BOOL)totalLoad {
    return (self.cosplay_list.count >= self.total_rows);
}
- (void)removeCosListItem:(KABCosListItem *)item {
    if ([self.cosplay_list indexOfObject:item] != NSNotFound) {
        [self.cosplay_list removeObject:item];
        --self.total_rows;
    }
}
- (void)addMoreWithData:(KABMineHomepage *)temp {
    self.total_rows = temp.total_rows;
    [self.cosplay_list addObjectsFromArray:temp.cosplay_list];
}
@end


@implementation KABMinePhotoItem
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"photoId" : @"id"};
}
@end
@implementation KABMinePhoto
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"photo_list" : [KABMinePhotoItem class]};
}
- (NSMutableArray<NSMutableArray *> *)sort_list {
    if (!_sort_list) {
        _sort_list = [self sortArray:nil formArray:_photo_list];
    }
    return _sort_list;
}
- (BOOL)totalLoad {
    return (self.photo_list.count >= self.total_rows);
}
- (void)addMoreWithData:(KABMinePhoto *)temp {
    self.total_rows = temp.total_rows;
    [self.photo_list addObjectsFromArray:temp.photo_list];
    self.sort_list = [self sortArray:self.sort_list formArray:temp.photo_list];
}
- (NSMutableArray<NSMutableArray *> *)sortArray:(NSMutableArray *)sortArray formArray:(NSArray *)array {
    NSMutableArray *bigArray = sortArray ? sortArray : [NSMutableArray array];
    for (KABMinePhotoItem *itemNew in array) {
        NSString *timeStr = [itemNew.publish_time substringWithRange:NSMakeRange(0, 7)];
        BOOL isAdd = NO;
        for (NSMutableArray<KABMinePhotoItem *> *array in bigArray) {
            if ([array.firstObject.publish_time hasPrefix:timeStr]) {
                [array addObject:itemNew];
                isAdd = YES;
            }
        }
        if (!isAdd) {
            NSMutableArray *arrayNew = [NSMutableArray arrayWithObject:itemNew];
            [bigArray addObject:arrayNew];
        }
    }
    return bigArray;
}
@end

@implementation KABMineSettingPushModel
- (BOOL)pushNotifyStateWithIndex:(NSInteger)index {
    NSArray<NSNumber *> *array = @[@(self.is_exhibition_notify), @(self.is_concern_notify), @(self.is_comment_notify)];
    if (index < array.count) {
        return [array[index] boolValue];
    }
    return NO;
}
- (void)setNotifyState:(BOOL)is_notified WithIndex:(NSInteger)index {
    switch (index) {
        case 0:
            self.is_exhibition_notify = is_notified;
            break;
        case 1:
            self.is_concern_notify = is_notified;
            break;
        case 2:
            self.is_comment_notify = is_notified;
            break;
        default:
            break;
    }
}
@end


@implementation KABMineInfo
@end


@implementation KABMineIMUserInfo
- (NSString *)imName {
    return self.is_merchant ? self.shop_name : self.user_nickname;
}
@end
